// https://gitee.com/Jeun1358/dtreejs-gen
//function Hadd(name,parent,link){console.log(d.add(name,parent,name,link))}
// tree -Jf => Get JSON
//r.add(id,pid,name,link);
const process = require('process');
var rf=require("fs"); 
target_file=(process.argv[2]);
// console.log("【注意】正在生成文件列表"+target_file+"的dTree树数据！");
var data=rf.readFileSync(target_file,"utf-8"); 
var JData=JSON.parse(data)
var target=(process.argv[3]);
try{
var p=''
function gpp(str){
  pn=str.split('/')
  var o='';
  for(q=0;q<(pn.length-2);q++){
    o=o+pn[q]+'/';
  }
  return o;
}
function gppf(str){
  pn=str.split('/')
  var o='';
  for(q=0;q<(pn.length-1);q++){
    o=o+pn[q]+'/';
  }
  return o;
}
function loada(a){
  for(i=0;i<a.length;i++){
    if(a[i].type=="D"||a[i].type=="directory"){
      var current_path=a[i].name+'/'
      var sp=current_path.split('/')
      current_name=sp[sp.length-2]
      parent_name=sp[sp.length-3]
      parent_path=gpp(current_path)
      if(i!=0){
        // console.log('【目录】'+current_path)
        // console.log('【目录名】'+current_name)
        // console.log('【目录路径】'+current_path)
        // console.log('【父目录名】'+parent_name)
        // console.log('【父目录路径】'+parent_path)
        //  //r.add('id','pid','name','link');
        if(target){p=p+`d.add("${current_path}","${parent_path}","${current_name}","${current_path}","${current_name}","${target}");`}
        else{p=p+`d.add("${current_path}","${parent_path}","${current_name}","${current_path}","${current_name}");`}
      }else{
        if(target){p=p+`d.add("${current_path}",-1,"${current_name}","${current_path}","${current_name}","${target}");`}
        else{p=p+`d.add("${current_path}",-1,"${current_name}","${current_path}","${current_name}");`}
      }
    }
    if(a[i].type=="F"||a[i].type=="file"){
      var current_path=a[i].name
      var sp=current_path.split('/')
      current_name=sp[sp.length-1]
      parent_name=sp[sp.length-2]+"/"
      parent_path=gppf(current_path)
      // console.log('【文件】'+current_path)
      var sp=a[i].name.split('/')
      // console.log('【文件名】'+current_name)
      // console.log('【父文件夹名】'+parent_name)
      if(!parent_path){
        // console.log('父文件夹就是根');
        if(target){p=p+`d.add("${current_path}",-1,"${current_name}","${current_path}","${current_name}","${target}");`}
        else{p=p+`d.add("${current_path}",-1,"${current_name}","${current_path}","${current_name}");`}
      }
      else{
        // console.log('【父文件夹路径】'+parent_path)
        if(target){p=p+`d.add("${current_path}","${parent_path}","${current_name}","${current_path}","${current_name}","${target}");`}
        else{p=p+`d.add("${current_path}","${parent_path}","${current_name}","${current_path}","${current_name}");`}
      }
    }
    // console.log('————————————')
  }
  // console.log('====================【输出】====================')
  console.log(`d=new dTree('d');`);
  console.log(p);
}
// console.log('====================【讯息】====================')
loada(JData)
}catch(e){console.log(e)}
//console.log(JData)